package thread;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Intersection {
    public static int[] intersection(int[] nums1, int[] nums2) {
        List<Integer> list1 = Arrays.stream(nums1).boxed().distinct().collect(Collectors.toList());
        List<Integer> list2 = Arrays.stream(nums2).boxed().distinct().collect(Collectors.toList());

        List<Integer> result = new ArrayList<>();

        for (int val : list1) {
            if (list2.contains(val)) {
                result.add(val);
            }
        }

        return result.stream().mapToInt(Integer::intValue).toArray();
    }

    public static void main(String[] args) {
        int[] nums1 = new int[]{4, 9, 5};
        int[] nums2 = new int[]{9, 4, 9, 8, 4};

        int[] result = intersection(nums1, nums2);
        for (int a : result) {
            System.out.print(a + " ");
        }
    }
}
